package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.packet.Presence;

public class Buddy2 extends JComponent {
	
	JLabel name;

	private static final long serialVersionUID = 1L;
	private RosterEntry _entry;
	private Presence _presence;
	public Buddy2(RosterEntry entry) {
		super();
		this._entry = entry;
		
		this.setLayout(new BorderLayout());
		
		name = new JLabel();
		if (entry.getName() != null) {
			name.setText(entry.getName());
		} else {
			name.setText(entry.getUser());
		}
		this.add(name, BorderLayout.WEST);
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		Graphics2D g2d = (Graphics2D)g.create();
		
		switch (_presence.getType()) {
		case available:
			if (_presence.getMode() != null) {
				switch (_presence.getMode()) {
				case available:
					g2d.setColor(Color.GREEN);
					break;
				case chat:
					g2d.setColor(Color.PINK);
					break;
				case dnd:
					g2d.setColor(Color.YELLOW);
					break;
				case xa:
					g2d.setColor(Color.GRAY);
					break;
				case away:
					g2d.setColor(Color.ORANGE);
					break;
				default:
					g2d.setColor(Color.DARK_GRAY);
					break;
				}
			} else {
				g2d.setColor(Color.GREEN);
			}
			break;
		case unavailable:
			g2d.setColor(Color.RED);
			break;
		default:
			g2d.setColor(Color.BLACK);
			break;
		}
		
		
		int[] x = new int[]{0, name.getWidth(), name.getWidth(), this.getWidth()};
		int[] y = new int[]{this.getHeight()-1, this.getHeight()-1, this.getHeight()/2,this.getHeight()/2};
		
		g2d.drawPolyline(x, y, x.length);
		g2d.fillRect(x[x.length-1] - 4, y[y.length-1]-4/2, 4, 4);
		g2d.dispose();
	}
	
	@Override
	public void setBackground(Color bg) {
		name.setBackground(bg);
		super.setBackground(bg);
	}


	@Override
	public void setForeground(Color fg) {
		name.setForeground(fg);
		super.setForeground(fg);
	}
	
	
	public RosterEntry getEntry() {
		return _entry;
	}

	public Presence getPresence() {
		return _presence;
	}

	public void setPresence(Presence _presence) {
		this._presence = _presence;
	}
}
